var _contextPath;

$(document).ready(function() {
	list();
	top.UppTabs.operator = top.getCurrentTagTitle();// 设置用户tab的标题值
});

/**
 * 查询用户列表
 */
function list() {
	_contextPath = $("#contextPath").val();
	$("#combocard_list").datagrid(
			{
				queryParams : {
					cardName : $("#cardName").val()
							&& $("#cardName").val() != null ? $("#cardName")
							.val() : "",status:-1
				},
				columns : [ [{
					field : 'comboCardId',
					title : '',
					hidden : true,
					align : 'center'
				},{
					field : 'comboCardName',
					title : '套餐名称',
					width : 100,
					formatter : function(value, row) {
						return '<a style="cursor: pointer;color: blue;" onclick="javascipt:info(this,\''+row.comboCardId+'\');" >'+value+'</a>'
					}
				},{
					field : 'createtime',
					title : '创建日期',
					width : 100,
					align : 'center',
					formatter : function(value, row, index) {
						return (new Date(Number(value))).dateConvert();
					}
				},{
					field : 'begintime',
					title : '套餐开始时间',
					width : 100,
					align : 'center',
					formatter : function(value, row, index) {
						return (new Date(Number(value))).dateConvert();
					}
				},{
					field : 'endtime',
					title : '套餐结束时间',
					width : 100,
					align : 'center',
					formatter : function(value, row, index) {
						return (new Date(Number(value))).dateConvert();
					}
				}, {
					field : 'comboCardCheckStatus',
					title : '审核状态',
					width : 100,
					align : 'center',
					formatter : function(value, row, index) {
						if (value == '0') {
							return '待审核';
						} else if (value == '1') {
							return '审核通过';
						} else if (value == '2') {
							return '审核不通过';
						} else if (value == '3') {
							return '删除';
						} else {
							return '';
						}
					}
				},  {
					field : 'comboCardTotalCopies',
					title : '总份数',
					width : 50,
					align : 'center'
				},  {
					field : 'comboCardSurplusCopies',
					title : '剩余份数',
					width : 50,
					align : 'center'
				}, {
					title : '操作',
					field : 'action',
					width : 120,
					formatter : function(value, row) {
//						alert(row.begintime > new Date());
						var str ="";
						if(row.isDelete){
							str=str==""?str:str+" | ";
							str += '<a style="cursor: pointer;color: red;" onclick="javascipt:removeC(\''+row.comboCardId+'\');" >删除</a>'
						}
						if(row.comboCardCheckStatus == '0' || row.comboCardCheckStatus == '2' ){
							str=str==""?str:str+" | ";
							str += '<a style="cursor: pointer;color: red;" onclick="javascipt:audit(this,\''+row.comboCardId+'\');" >审核</a>'
						}
						if(row.comboCardCheckStatus == '0' || row.comboCardCheckStatus == '2' || ((row.comboCardCheckStatus == '1' && row.comboCardTotalCopies ==row.comboCardSurplusCopies) 
								&& (row.comboCardCheckStatus == '1' && row.begintime > new Date()))){
							str=str==""?str:str+" | ";
							str += '<a style="cursor: pointer;color: red;" onclick="javascipt:edit(this,\''+row.comboCardId+'\');" >修改</a>'
						}
						if(row.comboCardCheckStatus == '1' && row.comboCardSurplusCopies >0 && new Date() < row.endtime){
							str=str==""?str:str+" | ";
							str += '<a style="cursor: pointer;color: red;" onclick="javascipt:sale(this,\''+row.comboCardId+'\');" >销售</a>'
						}
						if(row.comboCardCheckStatus == '1' && row.comboCardSurplusCopies<row.comboCardTotalCopies){
							str=str==""?str:str+" | ";
							str += '<a style="cursor: pointer;color: red;" onclick="javascipt:loadInfo(this,\''+row.comboCardId+'\');" >提取详情</a>'
						}
						return str;
					}
				}] ],
				autoRowHeight : true,
				fitColumns : true,
				pagination : true,
				rownumbers : true,
				singleSelect : true
			});
}

window.top["reload_combocard_list"]=function(){
	$('#combocard_list').datagrid( "load");
};
	
function getContextPath() {
	if ("" == _contextPath) {
		_contextPath = $("#contextPath").val();
	}
	return _contextPath;
}

/**
 * 搜索
 * 
 * @return
 */
function searchKey() {
	$('#combocard_list').datagrid('load', {
		cardName : $("#cardName").val() , status : $("#status").val()
	});
}

/**
 * 新增
 */
function add(obj) {
	var backHtml = $(obj).html();
	$(obj).html('套餐卡管理-新增');
	$(obj).attr('hiddenhref', getContextPath() + "iccard/combocard/toAddView");
	window.parent.menuClick(obj);
	$(obj).html(backHtml);
}

function edit(obj,id) {
//	var row = $("#user_list").datagrid("getSelected");
//	if (row) {
		var backHtml = $(obj).html();
		$(obj).html('套餐卡管理-编辑');
		$(obj).attr(
				'hiddenhref',
				getContextPath() + "iccard/combocard/toEditView?comboCardId="
						+ id);
		window.parent.menuClick(obj);
		$(obj).html(backHtml);
//	} else {
//		top.uppShow('请选择一行数据后，再进行编辑!');
//	}
}

function audit(obj,id) {
	var backHtml = $(obj).html();
	$(obj).html('套餐卡管理-审核');
	$(obj).attr(
			'hiddenhref',
			getContextPath() + "iccard/combocard/toAuditView?comboCardId="
					+ id);
	window.parent.menuClick(obj);
	$(obj).html(backHtml);
}

function sale(obj,id) {
	var backHtml = $(obj).html();
	$(obj).html('套餐卡管理-销售');
	$(obj).attr(
			'hiddenhref',
			getContextPath() + "iccard/combocard/toSaleView?comboCardId="
					+ id);
	window.parent.menuClick(obj);
	$(obj).html(backHtml);
}

function info(obj,id) {
	var backHtml = $(obj).html();
	$(obj).html('套餐卡管理-套餐详情');
	$(obj).attr(
			'hiddenhref',
			getContextPath() + "iccard/combocard/toInfoView?comboCardId="
					+ id);
	window.parent.menuClick(obj);
	$(obj).html(backHtml);
}

function loadInfo(obj,id) {
	var backHtml = $(obj).html();
	$(obj).html('套餐卡管理-提取详情');
	$(obj).attr(
			'hiddenhref',
			getContextPath() + "iccard/combocard/toGetInfoView?comboCardId="
					+ id);
	window.parent.menuClick(obj);
	$(obj).html(backHtml);
}

function removeC(comboCardId) {
	uppDialog.confirm(uppDialog.deleteMsgConfirm, function() {
	$.ajax({
		url : getContextPath() + 'iccard/combocard/remove',
		data : {
			comboCardId : comboCardId
		},
		type : 'POST',
		success : function(data) {
			top.uppShow(data.msgDesc);
			if ('0' == data.opResult) {
				window.location.href = getContextPath()
						+ "iccard/combocard/toListView";
			}
		}
	});
	});
}


